﻿using System;
using System.Linq;
using System.Windows.Forms;

namespace SwimmingCompetitions
{
    public partial class RaceControl : UserControl
    {
        public RaceControl()
        {
            InitializeComponent();
            _comboBoxRaceType.Format += (sender, e) =>
                                            {
                                                e.Value = e.Value.GetDescription<SwimmingGlobals.Mode>();
                                            };
            _comboBoxSex.Format += (sender, e) =>
                                       {
                                           e.Value = e.Value.GetDescription<SwimmingGlobals.Participants>();
                                       };
        }

        /// <summary>
        /// Passes selected race title.
        /// </summary>
        public event Action<int, SwimmingGlobals.Mode, SwimmingGlobals.Participants> AddRace;

        private void buttonAddClick(object sender, EventArgs e)
        {
            var participants = (SwimmingGlobals.Participants) _comboBoxSex.SelectedItem;
            var mode = (SwimmingGlobals.Mode) _comboBoxRaceType.SelectedItem;
            var distance = (int) _comboBoxDistance.SelectedItem;
            if (ActiveCompetitionMode.IsModeAvailable(participants, mode, distance))
            {
                if (AddRace != null)
                {
                    AddRace(distance, mode, participants);
                }
            }
            else
            {
                MessageBox.Show("Данный тип заплыва недоступен", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void load(object sender, EventArgs e)
        {
            _comboBoxRaceType.DataSource = Enum.GetValues(typeof(SwimmingGlobals.Mode)).Cast<SwimmingGlobals.Mode>().ToArray();
            _comboBoxSex.DataSource = new[] { SwimmingGlobals.Participants.Men, SwimmingGlobals.Participants.Women };
            _comboBoxDistance.DataSource = new[] { 50, 100, 200, 400 };
        }
    }
}
